<?php

namespace App\Admin\Controllers;

use App\Models\Battery;
use App\Models\BatterySetting;
use App\Models\SwitchModel;

use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
// use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use App\Models\ProjectUser;
use App\Models\Project;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;

class BatteryController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('电源列表')
            ->description('')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(new Battery(), function (Grid $grid) {
            $grid->column('name', '名称');
            $grid->column('huawei_id', '华为云设备ID');
            $list = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
            $res = Project::whereIn('projectId',$list)->pluck('projectname','projectId')->toArray();
            $grid->column('project_id', '项目')->using($res);
            $grid->text('address', '地址')->default('');
        });
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新建电源')
            ->description('创建')
            ->body($this->form());
    }

    public function form(){
        $builder = Battery::with('settings');
        return Form::make($builder, function (Form $form) {
            $form->text('name', '名称')->required();
            $list = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
            $res = Project::whereIn('projectId',$list)->pluck('projectname','projectId');
            $form->select('project_id', '所属项目')->options($res)->required();
            $form->text('huawei_id', '华为云设备ID')->required();
            $form->text('address', '地址')->default('');

            $form->hasMany('settings', function (Form\NestedForm $form) use ($list) {
                $form->radio('level', '优先级')->options(BatterySetting::LEVEL_TEXT)->required();
                $form->text('lowest', '最小值')->required();
                $switchGateways = SwitchModel::whereIn('project_id', $list)->where('pid', 0)->pluck('name', 'id')->toArray();
                $form->multipleSelect('switch_group', '绑定开关组')->options($switchGateways)
                ->saving(function ($value) {
                    return json_encode($value);
                })->required();
            });

            // $form->saving(function (Form $form) {
            //     $boxId = WeconHandler::getBoxIdByMachineCode($form->machine_code);
            //     if (empty($boxId)) {
            //         return $form->response()->error('无效的维控机器码');
            //     } else {
            //         $form->box_id = $boxId;
            //     }
            // });

            // $form->saved(function (Form $form, $result) {
            //     Cabinet::syncWeikong($form->getKey());
            // });

            $form->deleted(function (Form $form, $result) {
                if ($result) {
                    $data = $form->model()->toArray();
                    Battery::afterDeleted($data[0]['id']);
                }
            });
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('云盒编辑')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }

    public function show($id){
        return Show::make($id, new Battery(), function (Show $show) {
            $show->field('id', 'ID');
            $show->field('name','名称');

            $show->field('huawei_id', '华为云设备ID');
            $list = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
            $res = Project::whereIn('projectId',$list)->pluck('projectname','projectId')->toArray();
            $show->field('project_id', '项目')->using($res);
            $show->field('address', '地址')->default('');

            $show->relation('settings', '设置', function ($model) {
                $grid = new Grid(new BatterySetting);
                $grid->model()->where('battery_id', $model->id);
                // 设置路由
                $grid->setResource('settings');
                $grid->column('level', '优先级')->using(BatterySetting::LEVEL_TEXT);
                $grid->column('lowest', '最小值');
                $grid->column('switch_group', '绑定开关组')->display(function ($switchGroup) {
                    $str = '';
                    $switchGroupArr = json_decode($switchGroup, true);
                    foreach ($switchGroupArr as $one) {
                        $str .= SwitchModel::where('id', $one)->value('name');
                    }
                    return $str;
                });
                $grid->disableRefreshButton();
                $grid->disableCreateButton();
                $grid->disableRowSelector();
                $grid->disableActions();
                return $grid;
            });

            $show->field('created_at');
            $show->field('updated_at');
        });
    }
}